package algotithm.leetcode.listnode.test1634;

/**
 * @author zhouyanxiang
 * @Date 2021-02-2021/2/22-22:32
 * @URL https://leetcode-cn.com/problems/add-two-polynomials-represented-as-linked-lists/
 * @Title 1634. 求两个多项式链表的和
 */
class PolyNode {
    int coefficient, power;
    PolyNode next = null;

    PolyNode() {}
    PolyNode(int x, int y) {
        this.coefficient = x; this.power = y;
    }
    PolyNode(int x, int y, PolyNode next) {
        this.coefficient = x; this.power = y; this.next = next;
    }
}
public class Solution {
    public PolyNode addPoly(PolyNode l, PolyNode r) {
        if (l == null) {
            return r;
        }
        if (r == null) {
            return l;
        }
        if (l.power == r.power) {
            l.coefficient += r.coefficient;
            if (l.coefficient == 0) {
                return addPoly(l.next, r.next);
            }
            l.next = addPoly(l.next, r.next);

            return l;
        }else if (l.power > r.power) {
            l.next = addPoly(l.next, r);
            return l;
        } else {
            r.next = addPoly(l, r.next);
            return r;
        }
    }
}